﻿package org.atk.components.treegridview 
{
	import org.aswing.event.TreeModelListener;
	import org.aswing.tree.TreeModel;
	import org.aswing.tree.TreePath;
	import org.aswing.util.ArrayList;
	import org.aswing.util.ArrayUtils;
	
	/**
	 * ...
	 * @author AeTky
	 */
	public class TreeGridViewModel implements TreeModel{
		private var _ROOTCELL:TreeGridViewItem = null;
		
		private var searchForParent:Boolean = false;
		
		public function TreeGridViewModel(){
			_ROOTCELL = new TreeGridViewItem(null);
			_ROOTCELL.IsFolder = true;
			_ROOTCELL.IsOpen = true;
		}
		public function addTreeModelListener(l:TreeModelListener):void {
			
		}
		public function removeTreeModelListener(l:TreeModelListener):void {
			
		}
		
		private function findObject(startAt:Object, obj:Object): TreeGridViewItem {
			if (startAt == null) startAt = _ROOTCELL;
			if (!(startAt is TreeGridViewItem)) return null;
			
			var arr:Array = (startAt as TreeGridViewItem).Childs;
			var idx:int;
			for (idx = 0; idx < arr.length; idx++ ) {
				var item:TreeGridViewItem = arr[idx];
				if (item == obj) return item;
				
				item = findObject(item, obj);
				if (item != null) return item;
			}
			return null;
		}
		private function castObject(obj:Object):TreeGridViewItem{
			if (!(obj is TreeGridViewItem)) return null;
			return obj as TreeGridViewItem;
		}
		
		public function getChild(parent:Object, index:int):Object {
			if (index < 0) return null;
			
			var parentCell:TreeGridViewItem;
			if (searchForParent)
				parentCell = findObject(null, parent);
			else
				parentCell = castObject(parent);
			
			if (parentCell == null) return null;
			if (index >= parentCell.ChildCount) return null;
			return parentCell.Childs[index];
		}
		public function getChildCount(parent:Object):int {
			
			var parentCell:TreeGridViewItem;
			if (searchForParent)
				parentCell = findObject(null, parent);
			else
				parentCell = castObject(parent);
			
			if (parentCell == null) return -1;
			return parentCell.ChildCount;
		}
		public function getIndexOfChild(parent:Object, child:Object):int {
			
			var parentCell:TreeGridViewItem;
			if (searchForParent)
				parentCell = findObject(null, parent);
			else
				parentCell = castObject(parent);
			
			if (parentCell == null) return -1;
			
			return ArrayUtils.indexInArray(parentCell.Childs, child);			
		}
		public function getRoot():Object {
			return _ROOTCELL;
		}
		public function isLeaf(node:Object):Boolean {
			var aCell:TreeGridViewItem;
			if (searchForParent)
				aCell = findObject(null, node);
			else
				aCell = castObject(node);
			
			if (aCell == null) return undefined;
			return !aCell.IsFolder;
		}
		public function valueForPathChanged(path:TreePath, newValue:Object):void {
			
		}
		
		public function get SearchForParent():Boolean { return searchForParent; }
		
		public function set SearchForParent(value:Boolean):void {
			searchForParent = value;
		}
	}
	
}